<template>
  <div style="width: 100%;" class="card-area">
    <layout-content topTitle="查询" bottomTitle="评论列表">
      <!-- 搜索区域 -->
      <template v-slot:topCard>
        <search-view ref="searchView" :articleId="articleId" @search="search"/>
      </template>
      <!-- 表格区域 -->
      <template v-slot:bottomCard>
        <a-table
          ref="TableInfo"
          :columns="columns"
          rowKey="id"
          :dataSource="dataSource"
          :loading="loading"
          @change="handleTableChange"
          :pagination="pagination"
        >
          <template slot="operation" slot-scope="text, record">
            <a-button type="link"  @click="onDelete(record)" class="icon">删除</a-button>
          </template>
        </a-table>
      </template>
    </layout-content>
  </div>
</template>

<script>
import columns from './columns';
import modal from '~/tool/modal';
import apiURL from './requestURL';
import layoutContent from '@/components/tool/layout-content';
import searchView from './search-view';

export default {
  name: 'mainPage',
  components: { layoutContent, modal, searchView },
  data() {
    return {
      // 表头
      columns,
      // 接口url
      apiURL,
      articleId: '',

      dataSource: [],
      sortedInfo: null,
      paginationInfo: null,
      pagination: {
        size: 'small',
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      },
      loading: false
    };
  },
  created() {},
  mounted() {
    if (this.$route.query.articleId){
      this.articleId = this.$route.query.articleId.toString();
      this.$refs['searchView'].form.setFieldsValue({
        articleId: this.articleId
      });
      this.search({articleId: this.articleId});
    }else {
      this.search();
    }


  },
  methods: {
    /**
     * @desc: 删除记录
     * @param {object} 一条记录
     * @author: youzi
     */
    onDelete(record) {
      let that = this;
      this.$confirm({
        title: '确定删除吗?',
        content: '删除将不能查看哦！',
        centered: true,
        onOk() {
          that.$post(apiURL.deleteRecord,{id : record.id}).then(() => {
            that.$message.success('删除成功');
            that.search();
          });
        },
        onCancel() {}
      });
    },
    /**
     * @desc: 分页信息改变时的回调函数
     * @param 分页信息，过滤信息，排序信息
     * @author: youzi
     */
    handleTableChange(pagination, filters, sorter) {
      // 将这两个参数赋值给Vue data，用于后续使用
      this.paginationInfo = pagination;
      this.sortedInfo = sorter;
      /* this.fetch({
        sortField: sorter.field,
        sortOrder: sorter.order
      }); */
      // this.$refs['searchView'].handleSearch();
      this.search();
    },
    /**
     * @desc: 调用fetch的入口函数，先拿到当前列的排序和过滤信息
     * @author: youzi
     */
    search(params = {}) {
      let { sortedInfo } = this;
      let sortField, sortOrder;
      // 获取当前列的排序和列的过滤规则
      if (sortedInfo) {
        sortField = sortedInfo.field;
        sortOrder = sortedInfo.order;
      }
      this.fetch({
        ...params,
        sortField: sortField,
        sortOrder: sortOrder
      });
    },
    /**
     * @desc: 查询记录的函数
     * @param {object} params 包含查询条件，分页等信息
     * @author: youzi
     */
    fetch(params = {}) {
      this.loading = true;
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current;
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize;
        params.count = this.paginationInfo.pageSize;
        params.currentPage = this.paginationInfo.current;
      } else {
        // 如果分页信息为空，则设置为默认值
        params.count = this.pagination.defaultPageSize;
        params.currentPage = this.pagination.defaultCurrent;
      }
      this.$post(apiURL.getRecords, {
        ...params
      }).then(r => {
          let data = r.data.data;
          console.log(data)
          let pagination = { ...this.pagination };
          pagination.total = data.total;
          this.dataSource = data.rows;
          this.pagination = pagination;
          this.loading = false;
        })
        .catch(e => {
          console.error(e);
          this.loading = false;
        });
    }
  }
};
</script>

<style lang="less" scoped>
 .code-img {
  width: 4rem !important;
}
.reject-reason-content {
  text-align: center;
}
.divider {
  margin: 0 4px;
}
.desc {
  font-size: 20px !important;
  color: rgba(0, 0, 0, 0.6);
}
.icon {
  padding: 0;
  border: none;
  color: #1890ff;
}
.card-area {
  .extra-btn {
    width: 86px;
  }
}
.search-btn {
  margin-top: 8px;
}
.border-span {
  width: 2px;
  background-color: #cfd7de;
  height: 16px;
  margin-right: 2px;
  display: inline-block;
}
</style>
